using System;
using System.Security.Permissions;
using System.Web;
using PickGold.Charting.Utilities;

namespace PickGold.Charting
{
	[SRDescription("DescriptionAttributeCustomLabelsCollection_CustomLabelsCollection"), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class CustomLabelsCollection : ChartElementCollection<CustomLabel>
	{
		internal CustomLabelsCollection(Axis axis)
			: base(axis)
		{
		}

		public void Add(double labelsStep, DateTimeIntervalType intervalType)
		{
			this.Add(labelsStep, intervalType, 0.0, 0.0, "", 0, LabelMarkStyle.None);
		}

		public CustomLabel Add(double fromPosition, double toPosition, string text)
		{
			var item = new CustomLabel(fromPosition, toPosition, text, 0, LabelMarkStyle.None);
			base.Add(item);
			return item;
		}

		public void Add(double labelsStep, DateTimeIntervalType intervalType, string format)
		{
			this.Add(labelsStep, intervalType, 0.0, 0.0, format, 0, LabelMarkStyle.None);
		}

		internal CustomLabel Add(double fromPosition, double toPosition, string text, bool customLabel)
		{
			CustomLabel item = new CustomLabel(fromPosition, toPosition, text, 0, LabelMarkStyle.None);
			item.customLabel = customLabel;
			base.Add(item);
			return item;
		}

		public CustomLabel Add(double fromPosition, double toPosition, string text, int rowIndex, LabelMarkStyle markStyle)
		{
			CustomLabel item = new CustomLabel(fromPosition, toPosition, text, rowIndex, markStyle);
			base.Add(item);
			return item;
		}

		public void Add(double labelsStep, DateTimeIntervalType intervalType, string format, int rowIndex, LabelMarkStyle markStyle)
		{
			this.Add(labelsStep, intervalType, 0.0, 0.0, format, rowIndex, markStyle);
		}

		public CustomLabel Add(double fromPosition, double toPosition, string text, int rowIndex, LabelMarkStyle markStyle, GridTickTypes gridTick)
		{
			CustomLabel item = new CustomLabel(fromPosition, toPosition, text, rowIndex, markStyle, gridTick);
			base.Add(item);
			return item;
		}

		public void Add(double labelsStep, DateTimeIntervalType intervalType, double min, double max, string format, int rowIndex, LabelMarkStyle markStyle)
		{
			if ((((min == 0.0) && (max == 0.0)) && ((this.Axis != null) && !double.IsNaN(this.Axis.Minimum))) && !double.IsNaN(this.Axis.Maximum))
			{
				min = this.Axis.Minimum;
				max = this.Axis.Maximum;
			}
			double num = Math.Min(min, max);
			double num2 = Math.Max(min, max);
			this.SuspendUpdates();
			try
			{
				double d = num;
				double toPosition = 0.0;
				while (d < num2)
				{
					if (intervalType == DateTimeIntervalType.Number)
					{
						toPosition = d + labelsStep;
					}
					else if (intervalType == DateTimeIntervalType.Milliseconds)
					{
						toPosition = DateTime.FromOADate(d).AddMilliseconds(labelsStep).ToOADate();
					}
					else if (intervalType == DateTimeIntervalType.Seconds)
					{
						toPosition = DateTime.FromOADate(d).AddSeconds(labelsStep).ToOADate();
					}
					else if (intervalType == DateTimeIntervalType.Minutes)
					{
						toPosition = DateTime.FromOADate(d).AddMinutes(labelsStep).ToOADate();
					}
					else if (intervalType == DateTimeIntervalType.Hours)
					{
						toPosition = DateTime.FromOADate(d).AddHours(labelsStep).ToOADate();
					}
					else if (intervalType == DateTimeIntervalType.Days)
					{
						toPosition = DateTime.FromOADate(d).AddDays(labelsStep).ToOADate();
					}
					else if (intervalType == DateTimeIntervalType.Weeks)
					{
						toPosition = DateTime.FromOADate(d).AddDays(7.0 * labelsStep).ToOADate();
					}
					else if (intervalType == DateTimeIntervalType.Months)
					{
						toPosition = DateTime.FromOADate(d).AddMonths((int)labelsStep).ToOADate();
					}
					else
					{
						if (intervalType != DateTimeIntervalType.Years)
						{
							throw new ArgumentException(SR.ExceptionAxisLabelsIntervalTypeUnsupported(intervalType.ToString()));
						}
						toPosition = DateTime.FromOADate(d).AddYears((int)labelsStep).ToOADate();
					}
					if (toPosition > num2)
					{
						toPosition = num2;
					}
					ChartValueType valueType = ChartValueType.Double;
					if (intervalType != DateTimeIntervalType.Number)
					{
						if (this.Axis.GetAxisValuesType() == ChartValueType.DateTimeOffset)
						{
							valueType = ChartValueType.DateTimeOffset;
						}
						else
						{
							valueType = ChartValueType.DateTime;
						}
					}
					string text = ValueConverter.FormatValue(base.Common.Chart, this.Axis, null, d + ((toPosition - d) / 2.0), format, valueType, ChartElementType.AxisLabels);
					CustomLabel item = new CustomLabel(d, toPosition, text, rowIndex, markStyle);
					base.Add(item);
					d = toPosition;
				}
			}
			finally
			{
				this.ResumeUpdates();
			}
		}

		internal Axis Axis
		{
			get
			{
				return (base.Parent as Axis);
			}
		}
	}
}

